﻿using Simulation.Framework;
using System.Collections;
using UnityEngine;

namespace Easy.Game2d
{
    public class MonsterController:MonoBehaviour
    {
        #region data
        public Transform[] createMonsterPos;

        private Transform _player;

        private int _liveValue = 5;

        private int _nextValue = 1;

        private UILevelPanel _levelPanel;
        private Coroutine _waitCreateMonster;
        #endregion

        #region system
        private void Start()
        {
            SetLevel(1);
            CreateMonster();
        }
        #endregion

        #region Init
        public void InitMonster(Transform player)
        {
            _player=player;
        }
        #endregion

        #region next monster
        private void NextMonster(int value)
        {
            if (_waitCreateMonster != null) StopCoroutine(_waitCreateMonster);
            _waitCreateMonster = StartCoroutine(WaitCreateMonster(value));
        }
        private IEnumerator WaitCreateMonster(int value)
        {
            int valuetemp = value / 5;
            SetLevel(valuetemp);
            while (valuetemp>0)
            {
                CreateMonster();
                yield return new WaitForSeconds(3f);
                valuetemp--;
            }
        }
        #endregion

        #region logic
        private void CreateMonster()
        {
            for (int i = 0; i < 5; i++)
            {
                MonsterItem monster = CreateMonsterItem();
                monster.transform.position = createMonsterPos[i].position;
                monster.StartCor(_player, _nextValue, MonsterLiveValue);
            }
        }
        private MonsterItem CreateMonsterItem()
        {
            MonsterItem monster = SingleBehavior<ResManagerByResources>.getInstance().Load<MonsterItem>("Models/Monster");
           return UnityEngine.GameObject.Instantiate(monster);
        }
        private void MonsterLiveValue()
        {
            _liveValue--;
            if (_liveValue<=0)
            {
                if (_nextValue >8)
                {
                    if (_waitCreateMonster != null) StopCoroutine(_waitCreateMonster);
                    SetDialogue();
                }
                else
                {
                    _nextValue++;
                    _liveValue = _nextValue*5;
                    NextMonster(_liveValue);
                }
            }
        }
        #endregion

        #region ui
        private void SetLevel(int value)
        {
            if (_levelPanel == null) _levelPanel = SingleBehavior<PanelManager>.getInstance().Open<UILevelPanel>(PanelName._levelPanel);
            _levelPanel.Open();
            _levelPanel.SetLevel(value);
        }
        private bool _isfirst = true;
        private void SetDialogue()
        {
            if (_isfirst)
            {
                _isfirst = false;
                UIDialoguePanel dialoguePanel = SingleBehavior<PanelManager>.getInstance().Open<UIDialoguePanel>(PanelName._dialoguePanel);

                dialoguePanel.DialogueStep02(delegate { SingleBehavior<StepManager>.getInstance().PlayStep(StepName.stepBoss); });

            }

        }

        #endregion
    }
}
